# Load Data ---------------------------------------------------------------
library(haven)
library(psych)
mt <- read_dta("../Data/study3_raw.dta")


# Need for Chaos ----------------------------------------------------------
mt$nfc_teardown <- (mt$chaos1_1*-1+6)
mt$nfc_needchaos <- (mt$chaos1_2*-1+6)
mt$nfc_destroy <- (mt$chaos1_3*-1+6)
mt$nfc_upholdorder <- (mt$chaos1_4)
mt$nfc_workinside <- (mt$chaos1_5)
mt$nfc_protectinstits <- (mt$chaos1_6)
mt$nfc_greatthings <- (mt$chaos1_7)
mt$nfc_disastFun <- (mt$chaos2_1*-1+6)
mt$nfc_disastRebuild <- (mt$chaos2_2*-1+6)
mt$nfc_burnsociety <- (mt$chaos2_3*-1+6)
mt$nfc_burninstits <- (mt$chaos2_4*-1+6)
mt$nfc_clearrules <- (mt$chaos2_5)
mt$nfc_disastfear <- (mt$chaos2_6)
mt$nfc_chaosupset <- (mt$chaos2_7)
mt$nfc_respectproduct <- (mt$chaos2_8)

v <- grep("nfc_", names(mt), value = T)
omega(mt[, v], plot = F)

mt$nfc_f <- (mt$nfc_teardown + mt$nfc_needchaos + mt$nfc_destroy + mt$nfc_disastFun + mt$nfc_disastRebuild + mt$nfc_burnsociety + mt$nfc_burninstits)/7
mt$nfc_r <- (mt$nfc_upholdorder + mt$nfc_workinside + mt$nfc_protectinstits + mt$nfc_greatthings + mt$nfc_clearrules + mt$nfc_disastfear + mt$nfc_chaosupset + mt$nfc_respectproduct)/8

# cor(mt$nfc_f, mt$nfc_r, use = "complete.obs")

v_pw <- c("nfc_teardown", "nfc_needchaos", "nfc_destroy", "nfc_disastFun", "nfc_disastRebuild", "nfc_burnsociety", "nfc_burninstits")
v_nw <- c("nfc_upholdorder", "nfc_workinside", "nfc_protectinstits", "nfc_greatthings", "nfc_clearrules", "nfc_chaosupset", "nfc_respectproduct")
# omega(mt[, v_pw], plot = FALSE)
# omega(mt[, v_nw], plot = FALSE)

omega(mt[, c(v_pw, v_nw)], plot = F)

# Populism ----------------------------------------------------------------
mt$pop_fewints <- (mt$populism_full_1*-1+6)
mt$pop_crooked <- (mt$populism_full_2*-1+6)
mt$pop_nomethink <- (mt$populism_full_3*-1+6)
mt$pop_polsimprove <- mt$populism_full_4
mt$pop_yesmethink <- mt$populism_full_5
mt$pop_benefitall <- mt$populism_full_6

v <- grep("pop_", names(mt), value = T)
omega(mt[, v], plot = F)

mt$pop_f <- (mt$pop_fewints + mt$pop_crooked + mt$pop_nomethink)/3
mt$pop_r <- (mt$pop_polsimprove + mt$pop_yesmethink + mt$pop_benefitall)/3

v_pw <- c("pop_fewints", "pop_crooked", "pop_nomethink")
v_nw <- c("pop_polsimprove", "pop_yesmethink", "pop_benefitall")
# omega(mt[, v_pw], plot = FALSE)
# omega(mt[, v_nw], plot = FALSE)

omega(mt[, c(v_pw, v_nw)], plot = F)

# Political Violence ------------------------------------------------------
mt$viol_threatpols <- (mt$violence_1*-1+6)
mt$viol_bricks <- (mt$violence_2*-1+6)
mt$viol_stopbadgovt <- (mt$violence_3*-1+6)
mt$viol_bullets <- (mt$violence_4*-1+6)
mt$viol_noviol <- mt$violence_5
mt$viol_nonviolprot <- mt$violence_6
mt$viol_violunaccept <- mt$violence_7
mt$viol_notit4tat <- mt$violence_8

v <- grep("viol_", names(mt), value = T)
omega(mt[, v], plot = F)

mt$viol_f <- (mt$viol_threatpols + mt$viol_bricks + mt$viol_stopbadgovt + mt$viol_bullets)/4
mt$viol_r <- (mt$viol_noviol + mt$viol_nonviolprot + mt$viol_violunaccept + mt$viol_notit4tat)/4

v_pw <- c("viol_threatpols", "viol_bricks", "viol_stopbadgovt", "viol_bullets")
v_nw <- c("viol_noviol", "viol_nonviolprot", "viol_violunaccept", "viol_notit4tat")
# omega(mt[, v_pw], plot = FALSE, flip = F)
# omega(mt[, v_nw], plot = FALSE)

omega(mt[, c(v_pw, v_nw)], plot = F)

# Conspiratorial Thinking -------------------------------------------------
mt$consp_plots <- (mt$conspiracy_1*-1+6)
mt$consp_fewppl <- (mt$conspiracy_2*-1+6)
mt$consp_dkrun <- (mt$conspiracy_3*-1+6)
mt$consp_wars <- (mt$conspiracy_4*-1+6)
mt$consp_schoolexps <- mt$conspiracy_5
mt$consp_fewsects <- mt$conspiracy_6
mt$consp_complex <- mt$conspiracy_7
mt$consp_democ <- mt$conspiracy_8
mt$consp_US <- mt$conspiracy_9

v <- grep("consp_", names(mt), value = T)
omega(mt[, v], plot = F)

mt$consp_f <- (mt$consp_plots + mt$consp_fewppl + mt$consp_dkrun + mt$consp_wars)/4
# mt$consp_r <- (mt$consp_schoolexps + mt$consp_fewsects + mt$consp_complex + consp_democ + mt$consp_US)/5
mt$consp_r <- (mt$consp_schoolexps + mt$consp_complex + mt$consp_democ + mt$consp_US)/4

v_pw <- c("consp_plots", "consp_fewppl", "consp_dkrun", "consp_wars")
v_nw <- c("consp_schoolexps", "consp_complex", "consp_democ", "consp_US")
# # omega(mt[, v_pw], plot = FALSE)
# # omega(mt[, v_nw], plot = FALSE)

omega(mt[, c(v_pw, v_nw)], plot = F)

# Racial Resentment -------------------------------------------------------
mt$rr_specfavr <- (mt$raceresent_1*-1+6)
mt$rr_thard <- (mt$raceresent_2*-1+6)
mt$rr_dless <- mt$raceresent_3
mt$rr_pdisc <- mt$raceresent_4

v <- grep("rr_", names(mt), value = T)
omega(mt[, v], plot = F)

mt$rr_f <- (mt$rr_specfavr + mt$rr_thard)/2
mt$rr_r <- (mt$rr_dless + mt$rr_pdisc)/2

v_pw <- c("rr_specfavr", "rr_thard")
v_nw <- c("rr_dless", "rr_pdisc")
# omega(mt[, v_pw], plot = FALSE, nfactors = 1)
# omega(mt[, v_nw], plot = FALSE, nfactors = 1)

omega(mt[, c(v_pw, v_nw)], plot = F)

# Hostile Sexism ----------------------------------------------------------
mt$hs_control <- (mt$hs_full_1*-1+6)
mt$hs_exaggerate <- (mt$hs_full_2*-1+6)
mt$hs_leash <- (mt$hs_full_3*-1+6)
mt$hs_reasonable <- mt$hs_full_4
mt$hs_feministpower <- mt$hs_full_5
mt$hs_fewwomen <- mt$hs_full_6

v <- grep("hs_", names(mt), value = T)
v <- grep("full_", v, value = T, invert = T)
omega(mt[, v], plot = F)

mt$hs_f <- (mt$hs_control + mt$hs_exaggerate + mt$hs_leash)/3
mt$hs_r <- (mt$hs_reasonable + mt$hs_feministpower + mt$hs_fewwomen)/3

v_pw <- c("hs_control", "hs_exaggerate", "hs_leash")
v_nw <- c("hs_reasonable", "hs_feministpower", "hs_fewwomen")
# omega(mt[, v_pw], plot = FALSE, nfactors = 2)
# omega(mt[, v_nw], plot = FALSE, nfactors = 2)

omega(mt[, c(v_pw, v_nw)], plot = F)